import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Nivel1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Nivel1 extends World
{

    private int nivel;
    private  Heroe heroe;
    private  GreenfootSound intro;
    private  GreenfootSound lvl1;
    private  GreenfootSound lvl2;
    private  GreenfootSound lvl3;

    private Counter score;
    private Counter vidas;

    /**
     * Constructor for objects of class Nivel1.
     * 
     */

     public void introPlay()
    {
        intro.play();
    }
    
    public void detenlvl1()
    {
        lvl1.stop();
    }
    
     public void detenlvl2()
    {
        lvl2.stop();
    }
    
     public void detenlvl3()
    {
        lvl3.stop();
    }
    
    public Nivel1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 500, 1); 

        heroe = new Heroe();
        vidas=new Counter("Vidas: ");
        score=new Counter("Score: ");

        nivel=0;
        prepare(0);

        intro=new GreenfootSound("menu1.mp3");
        //intro.playLoop();
        lvl1=new GreenfootSound("lvl1.mp3");
        lvl2=new GreenfootSound("lvl2.mp3");
        lvl3=new GreenfootSound("lvl3.mp3");

    }

    public void started()
    {
        switch(nivel)
        {
            case 0:
            intro.play();
            break;
            case 1: lvl1.play();
            break;
            case 2: lvl2.play();
            break;
            case 3: lvl3.play();
            break;
        }
    }

    public void stopped()
    {
        intro.stop();
        lvl1.stop();

        lvl2.stop();

        lvl3.stop();

    }

    public void prepare(int dato)
    {
        addObject(vidas,50,20);
        addObject(score,50,40);

        switch(dato)
        {
            case 0:
            Menu();
            break;

            case 1:  
            intro.stop();
            lvl1.playLoop();
            vidas.setValue(heroe.getVidas());
            score.setValue(heroe.getScore());
            Nivel1();
            break;

            case 2:
            vidas.setValue(heroe.getVidas());
            score.setValue(heroe.getScore());
            lvl1.stop();
            lvl2.playLoop();
            Nivel2();
            break;

            case 3: 
            vidas.setValue(heroe.getVidas());
            score.setValue(heroe.getScore());
            lvl2.stop();
            lvl3.playLoop();
            Nivel3();
            break;

            case 4:
            lvl3.stop();
            break;

        }

    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    public void Menu()
    {
        setBackground("mega.png");
        Juega juega = new Juega();
        addObject(juega, 76, 76);
        juega.setLocation(454, 88);
        juega.setLocation(593, 73);
        juega.setLocation(147, 75);

        Ayuda ayuda = new Ayuda();
        addObject(ayuda, 147, 376);

        Creditos creditos= new Creditos();
        addObject(creditos,147,220);

        creditos.setLocation(129, 204);
        creditos.setLocation(134, 205);
        ayuda.setLocation(128, 339);
        creditos.setLocation(135, 231);
        ayuda.setLocation(136, 427);
        creditos.setLocation(140, 274);
        juega.setLocation(143, 62);

        Puntuacion puntuacion= new Puntuacion();
        addObject(puntuacion,147,299);
        creditos.setLocation(144, 212);
        creditos.setLocation(141, 210);

    }
    public void act()
    {

    }

    public void Nivel1()
    {

        addObject(heroe, 20, 465);
        heroe.setLocation(18, 458);
        setBackground("Nivel1.png");
        Piso piso = new Piso();
        addObject(piso, 84, 497);
        piso.setLocation(77, 488);
        piso.setLocation(78, 489);
        Piso piso2 = new Piso();
        addObject(piso2, 240, 496);
        piso2.setLocation(234, 489);
        Piso plataforma3 = new Piso();
        addObject(plataforma3, 397, 496);
        plataforma3.setLocation(390, 489);
        Piso plataforma4 = new Piso();
        addObject(plataforma4, 552, 496);
        plataforma4.setLocation(545, 489);
        Piso plataforma5 = new Piso();
        addObject(plataforma5, 699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 495);
        plataforma5.setLocation(699, 489);
        //  Heroe heroe = new Heroe();
        //addObject(heroe, 20, 465);
        //heroe.setLocation(18, 458);
        PlataformaMovil plataformamovil = new PlataformaMovil();
        addObject(plataformamovil, 135, 443);
        plataformamovil.setLocation(142, 417);
        Piso piso6 = new Piso();
        addObject(piso6, 630, 379);
        piso6.setLocation(623, 406);
        Piso piso7 = new Piso();
        addObject(piso7, 83, 303);
        piso7.setLocation(78, 301);
        Piso piso8 = new Piso();
        addObject(piso8, 629, 102);
        piso8.setLocation(626, 85);
        Piso piso9 = new Piso();
        addObject(piso9, 479, 91);
        piso9.setLocation(473, 85);
        Piso piso10 = new Piso();
        addObject(piso10, 238, 307);
        piso10.setLocation(233, 301);
        plataformamovil.setLocation(385, 211);
        Piso piso11 = new Piso();
        addObject(piso11, 476, 412);
        piso11.setLocation(471, 406);
        plataformamovil.setLocation(373, 351);
        PlataformaMovil plataformamovil2 = new PlataformaMovil();
        addObject(plataformamovil2, 331, 141);
        plataformamovil2.setLocation(328, 132);
        PlataformaMovil plataformamovil3 = new PlataformaMovil();
        addObject(plataformamovil3, 485, 235);
        plataformamovil3.setLocation(481, 223);
        Piso piso12 = new Piso();
        addObject(piso12, 78, 139);
        piso12.setLocation(78, 130);
        plataformamovil3.setLocation(408, 245);
        plataformamovil2.setLocation(247, 192);
        plataformamovil3.setLocation(512, 245);
        plataformamovil.setLocation(216, 372);
        plataformamovil.setLocation(217, 362);
        plataformamovil.setLocation(217, 363);
        piso6.setLocation(622, 423);
        piso11.setLocation(469, 423);
        plataformamovil.setLocation(272, 375);
        plataformamovil2.setLocation(103, 193);
        TNT tnt = new TNT();
        addObject(tnt, 583, 406);
        tnt.setLocation(581, 400);
        TNT tnt2 = new TNT();
        addObject(tnt2, 34, 286);
        tnt2.setLocation(34, 279);
        TNT tnt3 = new TNT();
        addObject(tnt3, 133, 113);
        tnt3.setLocation(128, 108);
        TNT tnt4 = new TNT();
        addObject(tnt4, 283, 471);
        tnt4.setLocation(274, 466);
        Nitro nitro = new Nitro();
        addObject(nitro, 512, 68);
        nitro.setLocation(509, 62);
        Nitro nitro2 = new Nitro();
        addObject(nitro2, 81, 178);
        nitro2.setLocation(190, 278);
        piso9.setLocation(473, 99);
        piso8.setLocation(627, 108);
        piso9.setLocation(473, 108);
        nitro.setLocation(508, 85);
        piso12.setLocation(77, 145);
        tnt3.setLocation(129, 123);
        plataformamovil2.setLocation(107, 219);
        plataformamovil3.setLocation(516, 272);
        piso7.setLocation(78, 330);
        piso10.setLocation(234, 329);
        plataformamovil.setLocation(274, 390);
        nitro2.setLocation(193, 306);
        tnt2.setLocation(34, 309);
        Transportador transportador = new Transportador();
        addObject(transportador, 667, 61);
        transportador.setLocation(661, 54);
        plataformamovil.setLocation(271, 373);
        plataformamovil2.setLocation(104, 206);
        plataformamovil2.setLocation(104, 211);
        transportador.setLocation(663, 42);
        piso8.setLocation(627, 94);
        piso9.setLocation(473, 94);
        nitro.setLocation(507, 72);
        tnt3.setLocation(126, 103);
        piso12.setLocation(76, 128);
        piso12.setLocation(76, 125);
        plataformamovil2.setLocation(110, 170);
        plataformamovil3.setLocation(454, 245);
        plataformamovil2.setLocation(129, 182);
        tnt2.setLocation(34, 274);
        nitro2.setLocation(188, 283);
        piso7.setLocation(78, 314);
        piso10.setLocation(233, 314);
        nitro2.setLocation(189, 291);
        tnt2.setLocation(44, 293);
        plataformamovil.setLocation(295, 374);
        plataformamovil.setLocation(302, 377);
        heroe.setLocation(18, 458);
        heroe.setLocation(18, 458);
        heroe.setLocation(18, 458);
        heroe.setLocation(18, 458);
        heroe.setLocation(18, 458);
        heroe.setLocation(18, 458);
        heroe.setLocation(18, 458);
        heroe.setLocation(18, 458);
        heroe.setLocation(20, 459);
        heroe.setLocation(26, 458);
        piso8.setLocation(627, 99);
        piso9.setLocation(473, 100);
        piso8.setLocation(627, 100);
        transportador.setLocation(663, 47);
        nitro.setLocation(511, 77);
        plataformamovil2.setLocation(235, 178);
        plataformamovil3.setLocation(482, 246);
        plataformamovil2.setLocation(162, 179);
        plataformamovil.setLocation(384, 360);
        plataformamovil3.setLocation(487, 233);

        Wolve wolve = new Wolve();
        addObject(wolve, 431, 68);
        wolve.setLocation(423, 63);
        Wolve wolve2 = new Wolve();
        addObject(wolve2, 425, 391);
        wolve2.setLocation(418, 385);
        Wolve wolve3 = new Wolve();
        addObject(wolve3, 32, 283);
        wolve3.setLocation(27, 276);
        wolve.setLocation(489, 62);
        wolve2.setLocation(644, 388);
        Vida vida = new Vida();
        addObject(vida, 692, 401);
        vida.setLocation(689, 395);
        Vida vida2 = new Vida();
        addObject(vida2, 24, 106);
        vida2.setLocation(18, 97);
        Vida vida3 = new Vida();
        addObject(vida3, 126, 291);
        vida3.setLocation(115, 286);
        vida.setLocation(687, 394);
        removeObject(vida3);
        removeObject(vida);
        Score score = new Score();
        addObject(score, 688, 399);
        score.setLocation(684, 395);
        Score score2 = new Score();
        addObject(score2, 126, 295);
        score2.setLocation(121, 288);
        Score score3 = new Score();
        addObject(score3, 499, 216);
        score3.setLocation(74, 96);
        Score score4 = new Score();
        addObject(score4, 558, 468);
        score4.setLocation(553, 462);
    }

    public void Nivel2()
    {

        setBackground("Nivel2.png");
        Piso piso = new Piso();
        addObject(piso, 82, 496);
        Piso piso2 = new Piso();
        addObject(piso2, 236, 496);
        Piso piso3 = new Piso();
        addObject(piso3, 390, 498);
        Piso piso4 = new Piso();
        addObject(piso4, 553, 496);
        Piso piso5 = new Piso();
        addObject(piso5, 556, 493);
        piso.setLocation(78, 490);
        piso2.setLocation(233, 491);
        piso3.setLocation(386, 491);
        piso2.setLocation(232, 490);
        piso3.setLocation(384, 490);
        piso5.setLocation(536, 490);
        piso4.setLocation(627, 490);

        addObject(heroe, 20, 465);
        heroe.setLocation(18, 458);

        Piso piso6 = new Piso();
        addObject(piso6, 84, 397);
        piso6.setLocation(78, 394);
        Piso piso7 = new Piso();
        addObject(piso7, 239, 401);
        piso7.setLocation(230, 394);
        Piso piso8 = new Piso();
        addObject(piso8, 662, 315);
        piso8.setLocation(658, 309);
        Piso piso9 = new Piso();
        addObject(piso9, 50, 212);
        piso9.setLocation(47, 204);
        Piso piso10 = new Piso();
        addObject(piso10, 628, 92);
        Piso piso11 = new Piso();
        addObject(piso11, 483, 98);
        piso11.setLocation(476, 92);
        piso8.setLocation(658, 290);
        Piso piso12 = new Piso();
        addObject(piso12, 83, 97);
        piso12.setLocation(80, 91);
        Transportador transportador = new Transportador(2);
        addObject(transportador, 35, 47);
        piso12.setLocation(77, 100);
        transportador.setLocation(30, 42);
        piso12.setLocation(76, 95);
        PlataformaMovil plataformamovil = new PlataformaMovil();
        addObject(plataformamovil, 480, 296);
        plataformamovil.setLocation(480, 291);
        PlataformaMovil plataformamovil2 = new PlataformaMovil();
        addObject(plataformamovil2, 205, 246);
        plataformamovil2.setLocation(203, 237);
        Wolve wolve = new Wolve();
        addObject(wolve, 679, 59);
        wolve.setLocation(677, 56);
        Wolve wolve2 = new Wolve();
        addObject(wolve2, 30, 362);
        wolve2.setLocation(26, 357);
        Shooter shooter = new Shooter();
        addObject(shooter, 680, 263);
        Shooter shooter2 = new Shooter();
        addObject(shooter2, 12, 176);
        shooter2.setLocation(8, 174);
        Jump jump = new Jump();
        addObject(jump, 405, 474);
        jump.setLocation(400, 467);
        Nitro nitro = new Nitro();
        addObject(nitro, 157, 376);
        nitro.setLocation(156, 372);
        Nitro nitro2 = new Nitro();
        addObject(nitro2, 124, 77);
        nitro2.setLocation(120, 71);
        transportador.setLocation(669, 41);
        Shooter shooter3 = new Shooter();
        addObject(shooter3, 11, 70);
        shooter3.setLocation(11, 67);
        Piso piso13 = new Piso();
        addObject(piso13, 326, 191);
        piso13.setLocation(324, 186);
        Jump jump2 = new Jump();
        addObject(jump2, 383, 172);
        jump2.setLocation(375, 164);
        jump2.setLocation(376, 165);
        Piso piso14 = new Piso();
        addObject(piso14, 667, 443);
        piso14.setLocation(663, 440);
        Shooter shooter4 = new Shooter();
        addObject(shooter4, 660, 417);
        shooter4.setLocation(670, 411);
        TNT tnt = new TNT();
        addObject(tnt, 528, 76);
        tnt.setLocation(521, 70);
        jump.setLocation(383, 468);
        Vida vida4 = new Vida();
        addObject(vida4, 71, 73);
        vida4.setLocation(67, 68);
        vida4.setLocation(638, 263);
        Score score5 = new Score();
        addObject(score5, 100, 370);
        score5.setLocation(97, 366);
        Score score6 = new Score();
        addObject(score6, 619, 416);
        score6.setLocation(612, 412);

    }

    public void Nivel3()
    {
        vidas.setLocation(600,20);
        score.setLocation(600,40);
        setBackground("Nivel3.png");
        Piso piso15 = new Piso();
        addObject(piso15, 399, 496);
        Piso piso16 = new Piso();
        addObject(piso16, 559, 492);
        Piso piso17 = new Piso();
        addObject(piso17, 655, 495);

        addObject(heroe, 20, 465);
        heroe.setLocation(18, 458);

        piso15.setLocation(387, 490);
        piso16.setLocation(528, 490);
        piso17.setLocation(655, 490);
        Piso piso18 = new Piso();
        addObject(piso18, 50, 408);
        Piso piso19 = new Piso();
        addObject(piso19, 672, 330);
        piso18.setLocation(54, 395);
        piso19.setLocation(672, 317);
        heroe.setLocation(23, 461);
        Jump jump = new Jump();
        addObject(jump, 178, 477);
        jump.setLocation(172, 467);
        Shooter shooter = new Shooter();
        addObject(shooter, 9, 368);
        Shooter shooter2 = new Shooter();
        addObject(shooter2, 686, 294);
        shooter2.setLocation(678, 289);
        PlataformaMovil plataformamovil = new PlataformaMovil();
        addObject(plataformamovil, 540, 198);
        Piso piso8 = new Piso();
        addObject(piso8, 270, 351);
        piso8.setLocation(262, 346);
        Piso piso9 = new Piso();
        addObject(piso9, 422, 352);
        piso9.setLocation(418, 346);
        Wolve wolve = new Wolve();
        addObject(wolve, 216, 317);
        wolve.setLocation(212, 309);
        Jump jump2 = new Jump();
        addObject(jump2, 91, 380);
        jump2.setLocation(87, 373);
        Jump jump3 = new Jump();
        addObject(jump3, 487, 331);
        jump3.setLocation(483, 324);
        Jump jump4 = new Jump();
        addObject(jump4, 657, 302);
        jump4.setLocation(649, 297);
        Piso piso10 = new Piso();
        addObject(piso10, 84, 211);
        piso10.setLocation(78, 202);
        Piso piso11 = new Piso();
        addObject(piso11, 239, 209);
        piso11.setLocation(234, 202);
        Shooter shooter3 = new Shooter();
        addObject(shooter3, 222, 180);
        shooter3.setLocation(222, 177);
        Wolve wolve2 = new Wolve();
        addObject(wolve2, 31, 170);
        wolve2.setLocation(29, 166);
        plataformamovil.setLocation(551, 137);
        plataformamovil.setLocation(551, 138);
        Wolve wolve3 = new Wolve();
        addObject(wolve3, 302, 459);
        wolve3.setLocation(299, 454);
        Score score = new Score();
        addObject(score, 601, 471);
        score.setLocation(603, 462);
        Score score2 = new Score();
        addObject(score2, 342, 324);
        score2.setLocation(340, 318);
        Piso piso12 = new Piso();
        addObject(piso12, 681, 414);
        piso12.setLocation(670, 407);
        Shooter shooter4 = new Shooter();
        addObject(shooter4, 606, 381);
        shooter4.setLocation(601, 379);
        Score score3 = new Score();
        addObject(score3, 130, 182);
        score3.setLocation(130, 176);

        Shooter shooter5 = new Shooter();
        addObject(shooter5, 322, 54);
        shooter5.setLocation(317, 49);
        Transportador transportador = new Transportador(3);
        addObject(transportador, 35, 14);
        transportador.setLocation(30, 10);
        transportador.setLocation(30, 14);

        Piso piso13 = new Piso();
        addObject(piso13, 83, 73);
        piso13.setLocation(76, 68);
        Piso piso14 = new Piso();
        addObject(piso14, 341, 74);
        piso14.setLocation(340, 68);
        shooter5.setLocation(322, 41);
        shooter5.setLocation(283, 44);

        Piso piso20 = new Piso();
        addObject(piso20, 82, 498);
        piso20.setLocation(76, 491);
        Piso piso21 = new Piso();
        addObject(piso21, 238, 496);
        piso21.setLocation(231, 490);
        Piso piso22 = new Piso();
        addObject(piso22, 502, 74);
        piso22.setLocation(494, 68);
        Piso piso23 = new Piso();
        addObject(piso23, 657, 74);
        piso23.setLocation(648, 68);
        Wolve wolve4 = new Wolve();
        addObject(wolve4, 670, 34);
        wolve4.setLocation(666, 33);
        Vida vida = new Vida();
        addObject(vida, 606, 45);
        vida.setLocation(604, 41);
        Score score4 = new Score();
        addObject(score4, 417, 47);
        score4.setLocation(411, 39);
        Nitro nitro = new Nitro();
        addObject(nitro, 270, 328);
        nitro.setLocation(262, 323);
        Nitro nitro2 = new Nitro();
        addObject(nitro2, 512, 52);
        nitro2.setLocation(513, 45);
        Nitro nitro3 = new Nitro();
        addObject(nitro3, 112, 49);
        nitro3.setLocation(108, 45);
        TNT tnt = new TNT();
        addObject(tnt, 419, 470);
        tnt.setLocation(413, 467);
        TNT tnt2 = new TNT();
        addObject(tnt2, 669, 388);
        tnt2.setLocation(669, 384);
        Vida vida2 = new Vida();
        addObject(vida2, 678, 468);
        vida2.setLocation(670, 464);

        heroe.setLocation(18, 458);
    }

    public int getNivel()
    {
        return nivel;
    }

    public void eliminaObjetos()
    {
        //nivel=0;
        int num,i=0;
        num=getObjects(null).size(); //Obtiene numero de objetos en el mundo
        Actor elimina;
        while(i<num)
        {
            elimina= (Actor) getObjects(null).get(0);
            removeObject(elimina);
            i++;
        }
    }

    public void incrementaNivel()
    {
        nivel++;
    }

    public void reseteaNivel()
    {
        nivel=0;
    }

    /**
     * Metodo que acomoda los records de mayor puntaje que se han generado en el juego
     */
    public void GameRecords()
    {

        java.util.List records=UserInfo.getTop(7);
        RecordUser usuario;
        UserInfo info;
        if(records!=null)
        {
            for(int i=0; i<records.size(); i++)
            {
                info=(UserInfo)records.get(i);
                usuario=new RecordUser((i+1)+".- "+info.getUserName(),info.getScore());
                usuario.addToWorld(50+35*i,this);
            }

            Letrero let;
            if(UserInfo.getMyInfo().getRank()>0)
            {
                let=new Letrero("Your Position:"+UserInfo.getMyInfo().getRank());
            }
            
            else
            {
                let=new Letrero("You aren't in the Records List");
                
            }
            
            this.addObject(let,20+(let.getImage().getWidth()/2),380);
        }
        else
        {
            Letrero letr=new Letrero("Whitout Scores...");
            this.addObject(letr,350,250);
        }
    }

    public void actualicemosPuntos()
    {
        if(UserInfo.isStorageAvailable())
        {

            UserInfo info=UserInfo.getMyInfo();
            if(heroe.getScore()>info.getScore())
            {
                info.setScore(heroe.getScore());
            }
            info.store();
        }
    }
    
    public void cambiaVidas(int num)
    {
        vidas.setValue(num);
    }
    
    public void cambiaScore(int num)
    {
         score.setValue(num);
    }
        

}


